package org.ikastola

import java.util.Date;

import ikastola.Constants;

class Service {

		Date classCreated
		Date classUpdated
		Integer deleted
		 
		static hasMany = [fees: Fee, discounts: Discount]
	
		static constraints = {
			classCreated nullable: true
			classUpdated nullable: true
			deleted inList: [0, 1]
		}
		
		def getMonthlyFees() {
			List<Fee> monthlyFees = new ArrayList<Fee>()
			for( f in fees) {
				if(f.period == Constants.FEE_PERIOD_MONTH) {
					monthlyFees.add(f)
				}	
			}
			return monthlyFees
		}
		
		def getAnnualFees() {
			List<Fee> annualFees = new ArrayList<Fee>()
			for( f in fees) {
				if(f.period == Constants.FEE_PERIOD_ANNUAL) {
					annualFees.add(f)
				}
			}
			return annualFees
		}
		
		def static findAllByFees(Fee fee) {
			def c = Service.createCriteria()
			def result = c.get {
				fees {
					idEq(fee.id)
				}
				eq("deleted",Constants.NOT_DELETED)
			}
			return result;
		}
		
		def static findAllByDiscounts(Discount disc) {
			def c = Service.createCriteria()
			def result = c.get {
				discounts {
					idEq(disc.id)
				}
				eq("deleted",Constants.NOT_DELETED)
			}
			return result;
		}
}
